package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.RegisterRecordMapper;
import com.cqjtu.drive.mapper.StudentMapper;
import com.cqjtu.drive.pojo.Grade;
import com.cqjtu.drive.pojo.RegisterRecord;
import com.cqjtu.drive.pojo.Student;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.RegisterRecordService;
import com.cqjtu.drive.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RegisterRecordServiceImpl implements RegisterRecordService {

    @Autowired
    private RegisterRecordMapper registerRecordMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Map<String,String> getRegisterRecordById(String registerRecordId) {
        RegisterRecord r = registerRecordMapper.getRegisterRecordById(registerRecordId);
        Map<String,String> map = new HashMap<>();
        map.put("reId",r.getReId());
        map.put("sId",r.getsId());
        map.put("rId",r.getReId());
        map.put("reAccept",Integer.toString(r.getReAccept()));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(r.getReDate());
        map.put("reDate",date);
        return map;
    }

    @Override
    public Response<String> insertRegisterRecord(RegisterRecord registerRecord) {
        try {
            List<RegisterRecord> list = registerRecordMapper.getAllRegisterRecords();
            if(list != null && !list.isEmpty()){
                String id = Integer.toString(Integer.parseInt(list.get(list.size() - 1).getReId()) + 1);
                registerRecord.setReId(id);
            }else {
                registerRecord.setReId("1");
            }
            registerRecord.setReDate(new Date());
            registerRecordMapper.insertRegisterRecord(registerRecord);
            return new Response<>(true, "Register record inserted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to insert register record: " + e.getMessage());
        }
    }

    @Override
    public Response<String> updateRegisterRecord(RegisterRecord registerRecord) {
        try {
            registerRecordMapper.updateReAccept(registerRecord);
            RegisterRecord r = registerRecordMapper.getRegisterRecordById(registerRecord.getReId());
            if(registerRecord.getReAccept() == 1){
                Student student = new Student();
                student.setsId(r.getsId());
                student.setsAccept(1);
                studentMapper.updatesAccept(student);
            }
            return new Response<>(true, "Register record updated successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to update register record: " + e.getMessage());
        }
    }

    @Override
    public Response<String> deleteRegisterRecord(String registerRecordId) {
        try {
            registerRecordMapper.deleteRegisterRecord(registerRecordId);
            return new Response<>(true, "Register record deleted successfully");
        } catch (Exception e) {
            return new Response<>(false, "Failed to delete register record: " + e.getMessage());
        }
    }

    @Override
    public List<Map<String, String>> getAllByReaccept(int op) {
        List<RegisterRecord> list = registerRecordMapper.getAllByReAccept(op);
        System.out.println("222222222222222222222222222222222222222");
        System.out.println(list);
        List<Map<String,String>> resp = new ArrayList<>();
        for(RegisterRecord r : list){
            Map<String,String> map = new HashMap<>();
            map.put("reId",r.getReId());
            map.put("sId",r.getsId());
            map.put("rId",r.getReId());
            map.put("reAccept",Integer.toString(r.getReAccept()));
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = dateFormat.format(r.getReDate());
            map.put("reDate",date);
            resp.add(map);
        }
        return resp;
    }
}
